<?php
/**
 * Created by PhpStorm.
 * User: AA
 * Date: 2016/12/14
 * Time: 18:17
 */

namespace Home\Logic;


class MessageLogic
{
    public function __construct()
    {
        $this->message = D('Message');
        $this->messageText = D('Message_text');
    }

    /**
     * C 端用户 消息记录
     * @param int $uid 用户ID
     * @param int $start 分页开始位置
     * @param int $length 分页长度
     * @return mixed array
     */

    public function getMessage( $uid = null,$start=0,$length=12 )
    {

        $data = $this->message
            ->join( 'message_text ON message.message_id = message_text.id')
            ->field( 'message.*, message_text.message')
            ->where( array( 'message.user_id'=>$uid, 'message.status_del'=>1 ) )
            ->order( 'message.create_time desc')
            ->limit( $start,$length )
            ->select( );
        return $data;
    }

    /**
     * B 端用户 所负责门店的消息记录
     * @param array $store_ids 门店ID集合
     * @param int $start 分页开始位置
     * @param int $length 分页长度
     * @return mixed mixed array
     */

    public function getStoreMessage( $store_ids,$start=0,$length=12 )
    {
        $data = $this->message
            ->join( 'message_text ON message.message_id = message_text.id')
            ->field( 'message.*, message_text.message')
            ->where( array( 'store_id'=> array( 'IN',$store_ids ),'status_del'=>1 ) )
            ->order( 'message.create_time desc')
            ->limit( $start,$length )
            ->select();
        return $data;
    }

    /**
     * 获取 B 端用户登录后 所负责门店的未读消息的数量
     * @param array $store_ids 门店ID集合
     * @return mixed int $num
     */

    public function getStoreMessageNum( $store_ids )
    {
        $num = $this->message
            ->where( array( 'store_id'=> array( 'IN',$store_ids ),'status_del'=>1,'status_read'=>1 ) )
            ->count();
        return $num;
    }

    /**
     * @param int $id
     * @param int $type 2->B 端用户获取数据 3->C 端用户获取数据
     * @param int $uid
     * @return mixed array
     */

    public function getContent( $id,$type,$uid = null )
    {
        // var_dump( );die();
        if ( $type == 2  )
        {
            // B 端用户获取数据 条件
            $store_ids = $_SESSION[$_SESSION['id'].'_storeid'];
            if ( !$store_ids ){ return false;die(); }
            $where = array( 'message.id'=>$id,'message.status_del'=>1,'message.store_id'=>array( 'IN',$store_ids ) );
        }
        elseif ( $type == 3 )
        {
            // C 端用户获取数据 条件
            $where = array( 'message.id'=>$id, 'message.status_del'=>1, 'message.user_id'=>$uid );
        }
        $data = $this->message
            ->join( 'message_text ON message.message_id = message_text.id')
            ->field( 'message.*, message_text.message')
            ->where( $where )
            ->order( 'message.create_time desc')
            ->find();
        return $data;
    }

    /**
     * 获取系统推送消息
     * @param int $uid
     * @return mixed
     */

    public function getSysteMessage( $uid = null,$start = null ,$length = null )
    {
        $data = $this->messageText
            ->where( array( 'type'=>1 ) )
            ->order( 'create_time desc' )
            ->select();
        return $data;
    }

    /**
     * 获取没有添加到消息中的系统推送消息
     * @param int $uid
     * @return mixed array
     */

    public function getNotMessage( $uid = null )
    {
        $sql_1 = 'SELECT m.message_id FROM message AS m WHERE t.id = m.message_id AND m.user_id = '.$uid;
        $sql_2 = 'SELECT t.id AS message_id FROM message_text AS t WHERE t.type = 1 AND NOT EXISTS( '.$sql_1.' )';
        $data = $this->message->query( $sql_2 );
        return $data;
    }

    /**
     * 添加 站内信息内容
     * @param array $data 插入数据库的数据
     * @return mixed int $id
     */

    public function setMessageText( $data )
    {
        $id = $this->messageText->add( $data );
        return $id;
    }

    /**
     * 添加 站内信息
     * @param array $data
     * @return mixed int $id
     */

    public function setMessage( $data )
    {
        if ( count( $data ) == count( $data, 1 ) )
        {
            $id = $this->message->add( $data );
            return $id;
        }
        else
        {
            $data = $this->message->addAll( $data );
            return $data;
        }

    }

    /**
     *  修改 站内信 阅读状态
     * @access public
     * @param array $data
     * @return int
     */

    public function setReadStatus( $data  )
    {
        $save['status_read'] = 2;
        $data = $this->message->where( $data )->field( 'status_read' )->save( $save );
        return $data;
    }

    /**
     *  修改 站内信 删除状态
     * @access public
     * @param array $data
     * @return int
     */

    public function setDelStatus( $data )
    {
        $save['status_del'] = 2;
        $data = $this->message->where( $data )->field( 'status_del' )->save( $save );
        return $data;
    }



}